﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Common
    {
    }

    public class Alpha
    {
        public void Beta()
        {
            while (true)
            {
                Console.WriteLine("Alpha.Beta is running in its own thread.");
            }
        }
    }

    internal class Account 
    {
        int balance;
        Random r = new Random();
        internal Account(int initial)
        {
            balance = initial;
        }

        internal int Withdraw(int amount)
        {
            if (balance < 0)
            {
                throw new Exception("Nagative Balance");
            }
            lock (this)
            {
                Console.WriteLine("Current Thread:" + Thread.CurrentThread.Name);
                if (balance >= amount)
                {
                    Thread.Sleep(5);
                    balance = balance - amount;
                    return amount;
                }
                else
                {
                    return 0; // transaction rejected
                }
            }
        }

        internal void DoTransactions()
        {
            for (int i = 0; i < 100; i++)
               Withdraw(r.Next(-50,100));
               
        }
        
        

    }

}
